/**
 * 权限相关的数据
 * Created by gcc on 2017/9/29.
 */
import Vue from "vue";
import {Tooltip} from "view-design";

const tableRowActionEdit = function (self) {
  return {
    title: '用户备注',
    key: 'note',
    align: 'center',
    width: 95,
    render: (h, params) => {
      return h('div', [h('Tooltip', {
        props: {content: params.row.note, placement: 'right', "max-width":"300"},
        style: {display: params.row.note ? "block" : "none"},
      }, '查看'),
      ]);
    }
  }
};
const tableRowStateTitle = function (self) {
  return {
    title: '订单状态',
    key: 'state',
    align: 'center',
    width: 120,
    render: (h, params) => {
      return h('div', self.orderStateDict.filter(item => item.value == params.row.state)[0].name);
    }
  }
};

const tableRowInnerNote = function (self) {
  return {
    title: '运营备注',
    key: 'id',
    align: 'center',
    width: 95,
    render: (h, params) => {
      if(params.row.innerNoteText){
        return (
          <div>
            < i-button type="primary" size="small"  style="margin-right:10px" onClick={() => {self.showInnerNote(params.row.id)}}>查看</i-button>
          </div>
        );
      } else {
        return <div></div>;
      }
    }
  }
};

const tableRowDeliveryTitle = function (self) {
  return {
    title: '物流方式',
    key: 'delivery_method',
    align: 'center',
    width: 110,
    render: (h, params) => {
      return (
        <div>
          <tag color={params.row.deliveryMethod===100?'green':'red'}>{params.row.deliveryMethod===100?'普通快递':'商业快递'}</tag>
        </div>
      );
    }
  }
};
const tableRowActionDetail = function (self) {
  return {
    title: '订单流水号',
    key: 'serialNumber',
    align: 'center',
    width: 205,
    render: (h, params) => {
      return  !self.$authority("ORDER-1-205")?h('div',params.row.serialNumber):
        h('div', [h('a', {
          style:{fontSize:"0.8rem"},
          on: {
            click: () => {
              self.showOrderDetail(params.row.id);
            }
          }
        }, params.row.serialNumber),
        ]) ;
    }
  }
};

const tableUserCol = function (self) {
  return {
    title: '用户',
    key: 'userName',
    align: 'center',
    width: 130,
    render: (h, params) => {
      let hideName = params.row.encodeName;
      if(!hideName){
        hideName = Vue.prototype.$tiyHideUsername(params.row.userName);
      }

      if(self.$authority("ORDER-1-218")){
        return(
          <div><a style="padding:6px;font-size:13px" onClick={() => {self.showUserContactInfo(params.row);}}>
                {hideName}
              </a>
          </div>
        );
      } else {
        return (<div>{hideName}</div>);
      }
    }
  };
}

// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

//页面状态相关
let status = {
  //显示order的 modal
  modalConfirmOrderShow : false,
  modalSearch: {
    isShow: false,
  },
  modalInnerNote: {
    isShow: false,
  },
  modalAction: {
    isShow: false,
    isLoading: false,
    title: '',
    content: '',
    type: 0,//0:备货1出库
    order: {
      id: ''
    }
  },
  modalUserContactInfo:{
    isShow:false,
    info:{
      userName:"",
      encodeName:"",
      phoneCode : "",
      mobile:"",
      email:"",
      facebook:"",
      instagram:"",
      web:""
    }
  },
  modalDetail:{
    isShow:false,
  },
  modalExportTransferExcel:{
    isShow: false
  },
  modalImportantNote: {
    isShow: false,
  },
  modalConfirm: {
    isShow: false,
    isLoading: false,
    title: '',
    content: '',
    okText: "",
    cancelText: "",
    onOk: function(){}
  },
};

// 和网络交互相关的参数
let params = {
  //搜索条件
  orderState: '',
  orderSerialNumber: '',
  userName: '',
  startTime:"",
  endTime:"",
  orderBy:'',
  exportOrder: {
    id: '',
    serialNumber: '',
    userName: ''
  },
  selectedOrders: [],
  isConfirmed : 1,
  waybill : "",
  transferExcelType: 0, //物流对接单格式
  modifyImportantNote:{
    orderIds:[],
    importantNote: "",
  },
  userEmail:''
};

// 页面的数据源
let data = {
  orderList: [],
  innerNoteList:[],
  dateRange: {
    disabledDate (date) {
      return date && date.valueOf() > Date.now() - 86400000;
    },
    shortcuts: [
      {
        text: '清空时间',
        value () {
          return [];
        }
      },
      {
        text: '最近一周',
        value () {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          return [start, end];
        }
      },
      {
        text: '最近一个月',
        value () {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          return [start, end];
        }
      },
      {
        text: '最近三个月',
        value () {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          return [start, end];
        }
      }
    ]
  },
};

let table = {
  //订单列表
  tableMain: {
    tableRowSelection: {
      type: 'selection',
      align: 'center',
      fixed: 'left',
      width: 55
    },
    tableRowIndex: {
      type: 'index',
      title: '#',
      key: 'index',
      fixed: 'left',
      align: 'center',
      width: 65
    },
    tableRowNumber: tableRowActionDetail,
    tableRowUserOrder: {
      title: '首单',
      key: 'isUserOrder',
      align: 'center',
      width: 95,
      render: (h, params) =>{
        let isFirstOrder;
        switch (params.row.firstOrder) {
          case 1:
            isFirstOrder = <tag color="red">首单</tag>;
            break;
          case 2:
            isFirstOrder = <div>复购</div>;
            break;
          case 3:
          default:
            isFirstOrder = <div></div>;
            break;
        }
        return isFirstOrder;
      }
    },
    tableRowNote: tableRowActionEdit,
    tableRowInnerNote: tableRowInnerNote,
    tableRowImportantNote: {
      title: '重要说明',
      key: 'importantNote',
      align: 'center',
      width: 95,
      render: (h, params) => {
        return h('div', [h('Tooltip', {
          props: {content: params.row.importantNote, placement: 'right', "max-width":"300"},
          style: {display: params.row.importantNote? "block" : "none",fontSize:"font-size:0.8rem;"},
        }, '查看'),
        ]);
      }
    },
    tableRowUser: tableUserCol,
    tableRowUserInviteCode: {
      title: '邀请码',
      key: 'userInviteCode',
      align: 'center',
      width: 90,
      render: (h, params) => {
       return (
         <div>
           <span style="font-size:0.8rem;">{params.row.userInviteCode}</span>
         </div>
       );
      }
    },
    tableRowCountry: {
      title: '国家',
      key: 'countryName',
      align: 'center',
      width: 120
    },
    tableRowCheckoutTime: {
      width: 145,
      title: '付款时间',
      key: 'checkoutTime',
      sortable: 'custom',
      sortType:"desc",
      render: (h, params) => {
        if(params.row.checkoutTime){
          return (
            <div>
              <span style="font-size:0.8rem;">{Vue.prototype.$tiyBJTime(params.row.checkoutTime)}</span>
            </div>
          );
        }else {
          return (
            <div></div>
          );
        }

      }
    },
    tableRowActualPayment: {
      title: '金额',
      key: 'actualPayment',
      align: 'center',
      width: 110,
      render: (h, params) => {
        return h('div', '$'+params.row.actualPayment);
      }
    },
    tableRowState: tableRowStateTitle,
    tableRowDevice: {
      title: '设备',
      align: 'center',
      key: 'deviceType',
      width: 100,
      render: (h, params) => {
        let device = '';
        switch(params.row.deviceType){
          case 1:
            device = 'Android';
            break;
          case 2:
            device = 'IOS';
            break;
          case 3:
            device = 'PC';
            break;
          case 4:
            device = 'Mobile PC';
            break;
          default:
            break;
        }
        return h('div', device);
      }
    },
  },
};

function prepareTables(self) {

  let titles = [
    table.tableMain.tableRowSelection,
    table.tableMain.tableRowIndex,
    table.tableMain.tableRowNumber(self),
    table.tableMain.tableRowUserOrder,
    table.tableMain.tableRowState(self),
    table.tableMain.tableRowNote(self),
    table.tableMain.tableRowInnerNote(self),
    table.tableMain.tableRowImportantNote,
    !self.$authority('ORDER-1-201')?null: table.tableMain.tableRowUser(self),
    !self.$authority('ORDER-1-222')?null: table.tableMain.tableRowUserInviteCode,
    !self.$authority('ORDER-1-202')?null: table.tableMain.tableRowCountry,
    table.tableMain.tableRowTime,
    table.tableMain.tableRowCheckoutTime,
    !self.$authority('ORDER-1-203')?null: table.tableMain.tableRowActualPayment,
    // !self.$authority('ORDER-1-204')?null: table.tableMain.tableRowDelivery(self),

    table.tableMain.tableRowDevice,
  ]

  return self.$authTableTitles(titles)

}


export  default  {
  tPage: page,
  tStatus: status,
  tParams: params,
  tTable: table,
  tableTitles:prepareTables,
  tData: data
};
